package com.nwpu.coursesupport.pagination;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class DialectFactory {
	/**
	 * Dialect instances map
	 */
	private static Map<DialectType, Dialect> DIALECTS = new ConcurrentHashMap<DialectType, Dialect>();

	/**
	 * Get dialect with dialect type
	 * 
	 * @param dialectType the dialect type
	 * @return
	 */
	public static Dialect getDialect(DialectType dialectType) {
		Dialect dialect = DIALECTS.get(dialectType);
		if (dialect == null) {
			dialect = newInstance(dialectType);
			DIALECTS.put(dialectType, dialect);
		}
		return dialect;
	}

	/**
	 * New dialect instance with dialect type
	 * 
	 * @param dialectType the dialect type
	 * @return
	 */
	private static Dialect newInstance(DialectType dialectType) {
		switch (dialectType) {
			case MYSQL:
				return new MySQLDialect();
			default:
				throw new UnsupportedOperationException();
		}
	}
}
